package io;

import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

public class SerializationTest {

	// method to serialize
	public static void serialize(){
	
		// create an object which will be 
		Dog dog1 = new Dog("big", new Collar("red",3));
		System.out.println("Dog: "+dog1);
	
		try{
			FileOutputStream fos = new FileOutputStream("dog.ser");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(dog1);
			oos.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	// method to serialize
	public static void deSerialize(){
		
		try{
			FileInputStream fos = new FileInputStream("dog.ser");
			ObjectInputStream oos = new ObjectInputStream(fos);
			Dog sameOldDog = (Dog)oos.readObject();
			System.out.println("sameOldDog: "+sameOldDog);
			oos.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	
	public static void main(String[] args){
		serialize();
		deSerialize();
	}
}

// Dog class
class Dog implements Serializable{
	String name;
	Collar collar;
	
	Dog(String name, Collar collar){
		this.name = name;
		this.collar = collar;
	}
	
	public String toString(){
		return "name: "+name+", collar color: "+collar.color+", collor size: "+collar.size;
	}
}

// Collar class
class Collar implements Serializable{
	String color;
	int size;
	
	Collar(String color, int size){
		this.color = color;
		this.size = size;
	}
}